package cn.qingxuntech.console.service

import cn.qingxuntech.console.entity.CalcDayReport
import cn.qingxuntech.console.entity.CalcHourReport
import cn.qingxuntech.console.entity.MediaIncome
import cn.qingxuntech.console.entity.MediaIncomeShow
import cn.qingxuntech.console.entity.ThirdMediaIncome
import cn.qingxuntech.console.mapper.CalcDayReportMapper
import cn.qingxuntech.console.mapper.CalcHourReportMapper
import cn.qingxuntech.console.mapper.MediaIncomeMapper
import cn.qingxuntech.console.mapper.ThirdMediaIncomeMapper
import cn.qingxuntech.console.model.request.ReportRequest
import cn.qingxuntech.console.model.response.PriceStat
import cn.qingxuntech.console.model.response.WeekStat
import com.mybatisflex.core.paginate.Page
import com.mybatisflex.core.query.QueryWrapper
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class ReportService {

    @Autowired
    lateinit var calcHourReportMapper: CalcHourReportMapper

    @Autowired
    lateinit var calcDayReportMapper: CalcDayReportMapper

    @Autowired
    lateinit var thirdMediaIncomeMapper: ThirdMediaIncomeMapper

    @Autowired
    lateinit var mediaIncomeMapper: MediaIncomeMapper


    fun calcHourPage(reportRequest: ReportRequest): Page<CalcHourReport> {

        return calcHourReportMapper.paginate(Page.of(reportRequest.current, reportRequest.size),
            QueryWrapper.create()
                .eq(CalcHourReport::dt, reportRequest.dt)
                .eq(CalcHourReport::hour, reportRequest.hour)
                .eq(CalcHourReport::adspaceId, reportRequest.adspaceId)
                .eq(CalcHourReport::mediaId, reportRequest.mediaId)
                .orderBy(CalcHourReport::dt, false)
                .orderBy(CalcHourReport::hour, false)
                .orderBy(CalcHourReport::conid, false)
        )
    }

    fun calcDayPage(reportRequest: ReportRequest): Page<CalcDayReport> {

        return calcDayReportMapper.paginate(Page.of(reportRequest.current, reportRequest.size),
            QueryWrapper.create()
                .eq(CalcDayReport::dt, reportRequest.dt)
                .eq(CalcDayReport::adspaceId, reportRequest.adspaceId)
                .eq(CalcDayReport::mediaId, reportRequest.mediaId)
                .orderBy(CalcDayReport::dt, false)
                .orderBy(CalcDayReport::conid, false)
        )
    }

    fun calcSpaceHour(reportRequest: ReportRequest): List<CalcHourReport> {

        return calcHourReportMapper.selectListByQuery(
            QueryWrapper.create()
                .eq(CalcHourReport::dt, reportRequest.dt)
                .le(CalcHourReport::hour, reportRequest.hour)
                .eq(CalcHourReport::adspaceId, reportRequest.adspaceId)
                .orderBy(CalcHourReport::dt, false)
                .orderBy(CalcHourReport::hour, false))
    }

    fun calcSpaceDay(reportRequest: ReportRequest): List<CalcDayReport> {

        return calcDayReportMapper.selectListByQuery(
            QueryWrapper.create()
                .between(CalcDayReport::dt, reportRequest.startDt, reportRequest.endDt)
                .eq(CalcDayReport::adspaceId, reportRequest.adspaceId)
                .orderBy(CalcDayReport::dt, false))
    }

    fun calcHourCompare(reportRequest: ReportRequest): List<CalcHourReport> {
        return calcHourReportMapper.selectListByQuery(
            QueryWrapper.create()
                .between(CalcHourReport::dt, reportRequest.startDt, reportRequest.endDt)
                .eq(CalcHourReport::adspaceId, reportRequest.adspaceId)
                .orderBy(CalcHourReport::dt, false)
                .orderBy(CalcHourReport::hour, true))
    }

    fun weekStat(): List<WeekStat> {
        return calcDayReportMapper.weekStat()
    }

    fun twoDay(): List<PriceStat> {
        return calcDayReportMapper.twoDay()
    }

    fun qsMediaIncome(reportRequest: ReportRequest): List<MediaIncomeShow> {

        val mediaIncomes = mediaIncomeMapper.selectListByQuery(
            QueryWrapper.create()
                .select("income_date, media_id, media_name, sum(income) as income")
                .between(MediaIncome::incomeDate, reportRequest.startDt, reportRequest.endDt)
                .`in`(MediaIncome::mediaId, reportRequest.mediaIds)
                .groupBy(MediaIncome::incomeDate, MediaIncome::mediaId, MediaIncome::mediaName)
                .orderBy(MediaIncome::incomeDate, false)
        )

        // 设置成本
        return mediaIncomes.map { mediaIncome ->
            val mediaIncomeShow = MediaIncomeShow()
            BeanUtils.copyProperties(mediaIncome, mediaIncomeShow)
            // 设置真实价格
            val thirdMediaIncome = thirdMediaIncomeMapper.selectOneByQuery(
                QueryWrapper.create()
                    .select("sum(income) as income")
                    .eq(ThirdMediaIncome::mediaId, mediaIncome.mediaId)
                    .eq(ThirdMediaIncome::incomeDate, mediaIncome.incomeDate)
            )
            mediaIncomeShow.realIncome = thirdMediaIncome.income
            mediaIncomeShow
        }
    }
}
